package stack;

/**
 * @author yunfei.shang
 * @date 2017/6/1.
 * @descreption 栈的简易实现
 */
public class CharStackDemo {
    private char[] charStack;
    private int index = -1;

    public static void main(String[] args) {
        String str = "hello world!";
        CharStackDemo charStackDemo = new CharStackDemo();
        int maxLength = str.length();
        charStackDemo.macCharLeng(maxLength);
        for (int i = 0; i < maxLength; i++) {
            if (!charStackDemo.isFull(maxLength)) {
                charStackDemo.push(str.charAt(i));
            }
        }
        for (int i = 0; i < charStackDemo.charStack.length; i++) {
            if (!charStackDemo.isEmpty()) {
                char ch = charStackDemo.pop();
                System.out.println(ch);
            }
        }
    }

    public void macCharLeng(int length) {
        charStack = new char[length];
    }

    public void push(char value) {
        charStack[++index] = value;
    }

    public char pop() {
        return charStack[index--];
    }

    public boolean isEmpty() {
        if (index == -1) {
            return true;
        }
        return false;
    }

    public boolean isFull(int maxLength) {
        if (index == maxLength) {
            return true;
        }
        return false;
    }
}

